package com.ossjk.api.controller;


import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonMethod;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.oa.accounting.entity.CwCapitals;
import com.ossjk.oa.accounting.service.ICwCapitalsService;
import com.ossjk.oa.accounting.service.ICwCpttypesService;
import com.ossjk.oa.accounting.service.MonthFinanceService;
import com.ossjk.oa.accounting.vo.CwCapitalsVo;
import com.ossjk.oa.accounting.vo.MonthFinanceVo;
import com.ossjk.oa.finance.service.IReimburseService;
import com.ossjk.oa.system.service.IDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

@Api(description = "财务月报接口")
@RestController
@RequestMapping("/api/Monthly")
public class MonthlyReport extends BaseController {
    @Autowired
    private ICwCapitalsService iCwCapitalsService;

    @Autowired
    private MonthFinanceService monthFinanceService;


    /*
    分析需求：需要传给前端来实现图表的一共有三样东西，
    var 月份 = [];
    var 收入总和 = []
    var 支出总和 = [];
	"type":["项目款","学费/定位费","工资","报销","劳务/课酬","租金/物业","水电费","卫生",
	"社保/公积金","税费","基建/物资","市场开拓",
	"员工活动","学生活动","综合","学员补贴","内部转款","借支款","提前还款",
	"介绍费","宽带/电话费","账务/手续费","利息","公司债","其它/未知"]
           @author:林镇彬
     * */
    //    按财务记录来拿取有数据的月份
    @GetMapping("/MonthNavigation")
    @ApiOperation(value = "获取所有资金流水的月份", notes = "传入mth属性，大于20号的默认本月，小于20号的默认上月--例如：mth=2020-07")
    @ApiImplicitParam(name = "mth", value = "具体年月", paramType = "query", dataType = "String")
    public Object MonthNavigation(String mth) {
        if (mth == null) {
            Calendar now = Calendar.getInstance();
            if (now.get(Calendar.DAY_OF_MONTH) > 20) {
                mth = CommonMethod.calMth(0, now.getTime()); // 默认本月
            } else {
                mth = CommonMethod.calMth(-1, now.getTime()); // 默认上月
            }
        }
        return responseSuccess(iCwCapitalsService.selectPreOrNextByMth(mth));
    }

    //    var 收入总和 = []
    @GetMapping("/allIncome")
    @ApiOperation(value = "收入总额", notes = "type：[\"项目款\",\"学费/定位费\",\"工资\",\"报销\",\"劳务/课酬\",\"租金/物业\",\"水电费\",\"卫生\",\n" +
            "\t\"社保/公积金\",\"税费\",\"基建/物资\",\"市场开拓\",\n" +
            "\t\"员工活动\",\"学生活动\",\"综合\",\"学员补贴\",\"内部转款\",\"借支款\",\"提前还款\",\n" +
            "\t\"介绍费\",\"宽带/电话费\",\"账务/手续费\",\"利息\",\"公司债\",\"其它/未知\"]")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "YearMonth", value = "具体年月", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "具体类型", paramType = "query", dataType = "String")
    })

    public ResponseBean<List<MonthFinanceVo>> income(String YearMonth, String type) {
//        where cptacount>=0 and typec like #{typec} and cptdate like #{cptdate}
//        GROUP BY cptdate ,typec
        Wrapper wrapper = Condition.create().isWhere(true).ge("cptacount", 0)
                .like("cptdate", YearMonth).like("typec", type)
                .groupBy("cptdate").groupBy("typec");
        List<CwCapitals> list = monthFinanceService.selectIncome(wrapper);

        return (ResponseBean<List<MonthFinanceVo>>) responseSuccess(list);

    }

    //     支出总和
    @GetMapping("/allExpenditure")
    @ApiOperation(value = "支出总额", notes = "type：[\"项目款\",\"学费/定位费\",\"工资\",\"报销\",\"劳务/课酬\",\"租金/物业\",\"水电费\",\"卫生\",\n" +
            "\t\"社保/公积金\",\"税费\",\"基建/物资\",\"市场开拓\",\n" +
            "\t\"员工活动\",\"学生活动\",\"综合\",\"学员补贴\",\"内部转款\",\"借支款\",\"提前还款\",\n" +
            "\t\"介绍费\",\"宽带/电话费\",\"账务/手续费\",\"利息\",\"公司债\",\"其它/未知\"]")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "YearMonth", value = "具体年月", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "具体类型", paramType = "query", dataType = "String")
    })
    public ResponseBean<List<MonthFinanceVo>> expenditure(String YearMonth, String type) {
//        where cptacount>=0 and typec like #{typec} and cptdate like #{cptdate}
//        GROUP BY cptdate ,typec
        Wrapper wrapper = Condition.create().isWhere(true).lt("cptacount", 0)
                .like("cptdate", YearMonth).like("typec", type)
                .groupBy("cptdate").groupBy("typec");
        List<CwCapitals> list = monthFinanceService.selectIncome(wrapper);
        return (ResponseBean<List<MonthFinanceVo>>) responseSuccess(list);

    }


}
